<?php
/**
 * 后台端---大转盘抽奖管理
 * @version：1.0.0
 * Date: 2023/05/10 11:17
 */

namespace app\backend\controllers\lottery_draw;

use App\backend\controllers\AdminBaseController;
use App\backend\models\BigWheelLogModel;
use App\backend\models\GoodsModel;
use App\backend\services\BigWheelPrizeServices;
use App\backend\services\BigWheelServices;
use App\backend\models\BigWheelModel;
use App\backend\models\BigWheelPrizeModel;

class BigWheelController extends AdminBaseController
{
    protected $class_name = 'BigWheel';

    public function index()
    {
        return 'bigWheel';

    }

    /**
     * 大转盘抽奖列表
     */
    public function getList()
    {
        //分页显示
        $data = request()->all();
        //列表
        $lists = BigWheelServices::getActivityList($data);
        return successJson('ok', $lists);
    }

    /**
     * 用户参与记录
     * @return void
     */
    public function lotteryRecordList()
    {
        //分页显示
        $data = request()->all();
        //活动id
        if (!isset($data['activity_id']) || empty($data['activity_id'])) {
            return  errorJson('参数错误');
        }

        //列表
        $lists = BigWheelServices::getPrizeRecordList($data);
        return successJson('ok', $lists);
    }


    /**
     * 编辑活动-获取活动信息
     */
    public function getActivity()
    {
        $id = request()->input('activity_id', 0);
        if (!$id) {
            return errorJson("参数不能为空！");
        }
        //获取信息
        $info = BigWheelServices::getActivityInfo($id);
        return successJson('ok', $info);
    }

    /**
     * 删除活动
     */
    public function delActivity()
    {
        $id = request()->input('activity_id', 0);
        if (!$id) {
            return errorJson("参数不能为空！");
        }

        //删除
        $res = BigWheelServices::deleteActivityData($id);
        if ($res['status'] != 0) {
            return successJson('删除成功');
        } else {
            return errorJson($res['msg']);
        }
    }

    /**
     * 基础设置
     * @return void
     */
    public function baseSettings()
    {
        //-------------基础设置
        $request = request()->all();
        //验证参数
        $param_res = BigWheelServices::CheckBaseParam($request);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //添加
        $res = BigWheelModel::getInstance()->addSave($param_res['data']);
        if ($res) {
            return successJson('新增成功', $res);
        } else {
            return errorJson("数据写入出错，请重试！");
        }

    }


    /**
     * 奖品设置
     * @return void
     */
    public function prizeSettings()
    {
        //------------奖品设置
        $request = request()->all();
        //验证参数
        $param_res = BigWheelServices::CheckBasePrizeParam($request);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }
        //验证活动
        $activity = BigWheelServices::activityExists($request['activity_id']);
        if (empty($activity)) {
            return errorJson("活动不存在");
        }

        //验证奖品数量限制
        $num_res = BigWheelServices::CheckActivityPrizeNum($request['activity_id'], $request['mode'],1);
        if ($num_res) {
            return errorJson($num_res['msg']);
        }

        //更新
        $res = BigWheelServices::activityDataSave($request['activity_id'], $param_res['data']);
        if ($res) {
            return successJson('新增成功');
        } else {
            return errorJson("数据写入出错，请重试！");
        }
    }

    /**
     * 抽奖次数--设置
     * @return void
     */
    public function numberOfDraws()
    {
        //----------抽奖次数
        $request = request()->all();
        //验证参数
        $param_res = BigWheelServices::CheckDrawsNumberParam($request);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //验证活动
        $activity = BigWheelServices::activityExists($request['activity_id']);
        if (empty($activity)) {
            return errorJson("活动不存在");
        }

        //更新
        $res = BigWheelServices::activityDataSave($request['activity_id'], $param_res['data']);
        if ($res) {
            return successJson('新增成功');
        } else {
            return errorJson("数据写入出错，请重试！");
        }

    }

    /**
     * 页面装修--设置
     * @return void
     */
    public function pageDecoration()
    {
        //----------页面装修
        $request = request()->all();
        //验证参数
        $param_res = BigWheelServices::CheckDecorationParam($request);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        $activity = BigWheelServices::activityExists($request['activity_id']);
        if (empty($activity)) {
            return errorJson("活动不存在");
        }

        //更新
        $res = BigWheelServices::activityDataSave($request['activity_id'], $param_res['data']);
        if ($res) {
            return successJson('新增成功');
        } else {
            return errorJson("数据写入出错，请重试！");
        }

    }

    /**
     * 编辑--更新活动基础信息
     * 备注：其他步骤的提交接口就是编辑
     * @return \Illuminate\Http\JsonResponse
     */
    public function modifyActivityBase()
    {
        $request = request()->all();
        //验证参数
        $param_res = BigWheelServices::CheckBaseParam($request);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        $activity = BigWheelServices::activityExists($request['activity_id']);
        if (empty($activity)) {
            return errorJson("活动不存在");
        }

        //更新
        $res = BigWheelServices::activityDataSave($request['activity_id'], $param_res['data']);
        if ($res) {
            return successJson('新增成功');
        } else {
            return errorJson("数据写入出错，请重试！");
        }

    }

    /**
     * 设置商品为奖品
     * @return \Illuminate\Http\JsonResponse
     */
    public function setGoodsToPrize()
    {
        $request = request()->all();
        //验证参数
        $param_res = BigWheelPrizeServices::setPrizeInfo(BigWheelPrizeServices::PRIZE_GODOS, $request);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //验证活动
        $activity = BigWheelServices::activityExists($request['activity_id']);
        if (empty($activity)) {
            return errorJson("活动不存在");
        }

        //验证奖品数量限制
        $num_res = BigWheelServices::CheckActivityPrizeNum($request['activity_id'], $request['mode'],0);
        if ($num_res) {
            return errorJson($num_res['msg']);
        }

        //添加
        $res = BigWheelPrizeServices::insertData($param_res['data']);
        if ($res) {
            return successJson('新增成功', $res);
        } else {
            return errorJson("数据写入出错，请重试！");
        }
    }

    /**
     * 设置微券为奖品
     * @return \Illuminate\Http\JsonResponse
     */
    public function setCouponToPrize()
    {
        $request = request()->all();
        //验证参数
        $param_res = BigWheelPrizeServices::setPrizeInfo(BigWheelPrizeServices::PRIZE_COUPON, $request);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //验证活动
        $activity = BigWheelServices::activityExists($request['activity_id']);
        if (empty($activity)) {
            return errorJson("活动不存在");
        }

        //验证奖品数量限制
        $num_res = BigWheelServices::CheckActivityPrizeNum($request['activity_id'], $request['mode'],0);
        if ($num_res) {
            return errorJson($num_res['msg']);
        }
        //添加
        $res = BigWheelPrizeServices::insertData($param_res['data']);
        if ($res) {
            return successJson('新增成功', $res);
        } else {
            return errorJson("数据写入出错，请重试！");
        }
    }

    /**
     * 设置积分为奖品
     * @return \Illuminate\Http\JsonResponse
     */
    public function setPointToPrize()
    {
        $request = request()->all();
        //验证参数
        $param_res = BigWheelPrizeServices::setPrizeInfo(BigWheelPrizeServices::PRIZE_POINT, $request);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //验证活动
        $activity = BigWheelServices::activityExists($request['activity_id']);
        if (empty($activity)) {
            return errorJson("活动不存在");
        }

        //验证奖品数量限制
        $num_res = BigWheelServices::CheckActivityPrizeNum($request['activity_id'], $request['mode'],0);
        if ($num_res) {
            return errorJson($num_res['msg']);
        }

        //添加
        $res = BigWheelPrizeServices::insertData($param_res['data']);
        if ($res) {
            return successJson('新增成功', $res);
        } else {
            return errorJson("数据写入出错，请重试！");
        }
    }

    /**
     * 设置余额为奖品
     * @return \Illuminate\Http\JsonResponse
     */
    public function setBalanceToPrize()
    {
        $request = request()->all();
        //验证参数
        $param_res = BigWheelPrizeServices::setPrizeInfo(BigWheelPrizeServices::PRIZE_BALANCE, $request);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //验证活动
        $activity = BigWheelServices::activityExists($request['activity_id']);
        if (empty($activity)) {
            return errorJson("活动不存在");
        }

        //验证奖品数量限制
        $num_res = BigWheelServices::CheckActivityPrizeNum($request['activity_id'], $request['mode'],0);
        if ($num_res) {
            return errorJson($num_res['msg']);
        }

        //添加
        $res = BigWheelPrizeServices::insertData($param_res['data']);
        if ($res) {
            return successJson('新增成功', $res);
        } else {
            return errorJson("数据写入出错，请重试！");
        }
    }

    /**
     * 获取活动奖品列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function getActivityPrizeList()
    {
        $activity_id = request()->input('activity_id', 0);
        if (!$activity_id) {
            return errorJson("参数不能为空！");
        }
        //列表
        $lists = BigWheelPrizeServices::getPrizeList($activity_id);
        return successJson('ok', $lists);
    }

    /**
     * 编辑--获取活动奖品
     * @return \Illuminate\Http\JsonResponse
     */
    public function getActivityPrize()
    {
        $prize_id = request()->input('prize_id', 0);
        if (!$prize_id) {
            return errorJson("参数不能为空！");
        }
        //获取信息
        $info = BigWheelPrizeServices::getPrizeInfo($prize_id);
        return successJson('ok', $info);
    }

    /**
     * 编辑--更新活动信息
     * @return \Illuminate\Http\JsonResponse
     */
    public function modifyActivityPrize()
    {
        $request = request()->all();
        //验证参数
        $param_res = BigWheelPrizeServices::checkModifyParam($request);
        if ($param_res['status'] != 0) {
            return errorJson($param_res['status'] . $param_res['msg']);
        }

        //更新数据
        $res = BigWheelPrizeServices::updatePrizeData($param_res['data']);
        if ($res) {
            return successJson('更新成功', $res);
        } else {
            return errorJson("数据写入出错，请重试！");
        }
    }

    /**
     * 删除活动奖品
     * @return \Illuminate\Http\JsonResponse
     */
    public function delActivityPrize()
    {
        $prize_id = request()->input('prize_id', 0);
        if (!$prize_id) {
            return errorJson("参数不能为空！");
        }

        //删除
        $res = BigWheelPrizeServices::deletePrizeData($prize_id);
        if ($res['status'] == 0) {
            return successJson('删除成功');
        } else {
            return errorJson($res['msg']);
        }
    }

    /**
     * 获取可以设置为奖品的商品列表
     * 商品为全部商品 --所有商户
     * @return \Illuminate\Http\JsonResponse
     */
    public function getPrizeGoodsList()
    {
        //分页显示
        $data = request()->all();
        //列表
        $lists = BigWheelServices::setPrizeGoodsList($data);
        return successJson('ok', $lists);
    }

    /**
     * 获取可以设置为奖品的微券列表
     * 券也为全部可用的券--通用--专用
     * @return \Illuminate\Http\JsonResponse
     */
    public function getPrizeCouponList()
    {
        //分页显示
        $data = request()->all();
        //列表
        $lists = BigWheelServices::setPrizeCouponList($data);
        return successJson('ok', $lists);
    }

    /**
     * 获取可以设置为奖品的微券列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function  addLotteryRecord()
    {
        $data = request()->all();
        $uid = 9;
        $saveDate = [];
        $saveDate['uid'] = isset($data['activity_id'])&&!empty($data['activity_id'])?:$uid;
        $saveDate['activity_id'] = isset($data['activity_id'])&&!empty($data['activity_id'])?:'1';
        $saveDate['prize_id'] = isset($data['prize_id'])&&!empty($data['prize_id'])?:'2';
        $saveDate['name'] = isset($data['name'])&&!empty($data['name'])?:'新人10元优惠券';
        $saveDate['content'] = '新人10元优惠券';
        //保存
        $res = BigWheelLogModel::getInstance()->insertSave($saveDate);
        return successJson('ok', $res);
    }


    /**
     * 大转盘抽奖--测试方法
     * 抽奖概率算法
     */
    public function goLotteryResults(){
        //获取活动id
        $activity_id = 2;
        //获取抽奖结果
        $prize =  BigWheelServices::getLotteryResults($activity_id);
        var_dump($prize);
        die;
        $prize_num = $prize['pid'];
        echo "得出结果："."  奖品id：".$prize_num.'  '.$prize['name'];
        die();
    }

    /**
     * 大转盘抽奖逻辑--用户抽奖
     * @return void
     */
    public function goLottery(){
        $activity_id = 2;
        $field = ['*'];
        $activity_info = BigWheelModel::getInstance()->getInfo(['activity_id' => $activity_id], $field);
        if (empty($activity_info)) {
            return $activity_info;
        }

        /**
         * 一.用户抽奖逻辑（上限次数0为无上限）
         * 规则周期--按天重复    活动期间触发一次
         * 1.免费获得（ ）次数
         * 2.使用（ ）积分兑换次数，上限（ ）次
         * 3.分享好友获的1次，上限（ ）次 （0无上限）
         * 4.在商城消费满（ ），获得1次数，上限（ ）次
         * 判断是否可以抽奖，可以则获取抽奖结果。保存。
         *
         * 二.抽奖逻辑保存
         */
        //用户抽奖次数判断
        $res = BigWheelServices::goLottery($activity_id);
        if ($res['status'] == 0) {
            return successJson('成功',$res['data']);
        } else {
            return errorJson($res['msg']);
        }
    }
    /**
     * 大转盘--用户抽奖次数
     * @return void
     */
    public function getUserLotteryNum(){
        $activity_id = 2;
        $uid = 9;
        //获取用户抽奖次数
        $res = BigWheelServices::getUserLotteryNum($activity_id,$uid);
        if ($res['status'] == 0) {
            return successJson('成功',$res['data']);
        } else {
            return errorJson($res['msg']);
        }
    }
}
